<template>
	<view class="mine-page page">
		<view class="info-block">
			<image src="https://jinchang-job-1306862033.cos.ap-chengdu.myqcloud.com/miniprogram-static/complain.png"
				mode="widthFix" style="width: 100%;margin-bottom: 20rpx;">
			</image>
			
			<view class="tips">
				<view>此投诉为本小程序自有投诉渠道，非微信官方投诉渠道</view>
			</view>
			
			<view class="info-list" style="margin-top: 20rpx;">
				<view class="form-item">
					<view class="name-item">
						<view class="must">*</view>
						<view class="name">姓名</view>
					</view>
					<view class="form-content">
						<input v-model="form.name" class="form-crl1" placeholder-style="color:#000;"
							placeholder-class="input-placeholder" placeholder="请输入您的姓名" />
					</view>
				</view>
				<view class="form-item">
					<view class="name-item">
						<view class="must">*</view>
						<view class="name">手机号</view>
					</view>
					<input v-model="form.phone" :disabled="true" class="form-crl1" placeholder-style="color:#000;"
						placeholder-class="input-placeholder" />
				</view>
			</view>
			<view class="info-list">
				<view class="form-item">
					<view class="name-item">
						<view class="must">*</view>
						<view class="name">描述</view>
					</view>
					<view class="form-content">
						<textarea placeholder="请简述您的投诉内容..." v-model="form.description" style="height: 200rpx;"
							placeholder-class="input-placeholder" placeholder-style="color:#000;" maxlength="300" />
					</view>
				</view>
				<view class="form-item">
					<view class="name-item">
						<view class="name">地址</view>
					</view>
					<view class="form-content" @click="chooseLocation">
						<input v-model="form.address" :disabled="true" class="form-crl1" placeholder-style="color:#000;"
							placeholder-class="input-placeholder" placeholder="请选择您的地址" />
						<view class="right">
							<view class="iconfont icon-gengduo"></view>
						</view>
					</view>
				</view>
				<view class="form-item">
					<view class="name-item">
						<view class="name">上传图片({{form.imageUrl.length}}/2)</view>
					</view>
					<view class="upload">
						<view v-for="(item,index) in form.imageUrl" class="image-item">
							<image mode="aspectFit" :src="item" @click="previewImg(item)" class="img">
							</image>
							<view @click="closeImg(index,1)" style="display: inline;">
								<view class="iconfont">
									<view class="delete icon-guanbi"></view>
								</view>
							</view>
						</view>
						<view class="upload-content" v-if="form.imageUrl.length<2" @click="chooseImg">
							<image src="https://www.mashangjiuye.cn/file/91885/20230302173239.png" class="upload">
							</image>
						</view>
					</view>
				</view>
				<view class="form-item">
					<view class="name-item">
						<view class="name">上传视频</view>
					</view>
					<view class="upload">
						<view v-for="(item,index) in form.videoUrl" class="image-item">
							<video id="myVideo" class="img" style="width: 190rpx;height: 200rpx;border-radius: 20rpx;"
								:src="item" show-fullscreen-btn></video>
							<view @click="closeImg(index,2)" style="display: inline;">
								<view class="iconfont">
									<view class="delete icon-guanbi"></view>
								</view>
							</view>
						</view>
						<view class="upload-content" v-if="form.videoUrl.length<1" @click="chooseMedia">
							<image src="https://www.mashangjiuye.cn/file/91885/20230302173239.png" class="upload">
							</image>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="btn-item">
			<view class="btn">
				<view class="btn normal" @click="submit"> 提交</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		validate
	} from '@/util/validate.js'
	import config from '@/config.js';
	import {
		getStorage,
		showToast
	} from '@/util/auth';
	import {
		saveComplaintReport
	} from '@/api/complain.js'
	export default {
		data() {
			return {
				form: {
					name: '',
					phone: '',
					address: '',
					latitude: '',
					longitude: '',
					description: '',
					imageUrl: [],
					videoUrl: []
				},
				rules: {
					name: {
						name: '姓名',
						required: true
					},
					description: {
						name: '描述',
						required: true
					},
				}
			}
		},

		onLoad() {

		},
		methods: {
			//选择位置
			chooseLocation() {
				uni.chooseLocation({
					success: (e) => {
						this.form.address = e.address;
						this.form.latitude = e.latitude;
						this.form.longitude = e.longitude;
					},
				})
			},
			chooseImg() {
				uni.chooseImage({
					count: 6, // 最多可以选择的图片张数，默认9
					sizeType: ['compressed'],
					sourceType: ['album', 'camera'],
					uplodaType: 3,
					success: res => {
						uni.uploadFile({
							url: config.requestUrl + '/upload?uploadType=2', //post请求的地址
							filePath: res.tempFilePaths[0],
							name: 'file',
							formData: {},
							header: {
								// Authorization: that.authtoken
							},

							success: (myres) => {
								const data = JSON.parse(myres.data);
								if (this.form.imageUrl.length === 0) {
									this.form.imageUrl = this.form.imageUrl.concat(data.data)
								} else if (this.form.imageUrl.length < 3) {
									this.form.imageUrl = this.form.imageUrl.concat(data
										.data) //concat追加到数组
								}
							}
						})
					}
				})
			},
			chooseMedia(e) {
				var that = this;
				uni.chooseVideo({
					sourceType: ['camera', 'album'],
					success: function(res) {
						uni.uploadFile({
							url: config.requestUrl + '/upload?uploadType=2', //post请求的地址
							filePath: res.tempFilePath,
							name: 'file',
							formData: {},
							header: {
								// Authorization: that.authtoken
							},
							success: (myres) => {
								const data = JSON.parse(myres.data);
								that.form.videoUrl = that.form.videoUrl.concat(data.data)
							}
						})
					}
				});
			},
			// 删除图片
			closeImg(index, e) {
				var that = this;
				uni.showModal({
					title: '提示',
					content: '是否删除当前文件',
					success: function(res) {
						if (res.confirm) {
							if (e == 1) {
								that.form.imageUrl.splice(index, 1);
							} else if (e == 2) {
								that.form.videoUrl.splice(index, 1);
							}
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			submit() {
				let form = JSON.parse(JSON.stringify(this.form));
				if (form.imageUrl.length > 0) {
					form.imageUrl = form.imageUrl.join(',')
				}
				if (form.videoUrl.length > 0) {
					form.videoUrl = form.videoUrl.join(',')
				}
				if (validate(form, this.rules)) {
					saveComplaintReport(form).then(res => {
						uni.showToast({
							title: '提交成功',
							icon: 'none',
							duration: 1000,
							mask: true,
							success: function() {
								setTimeout(function() {
									uni.navigateBack();
								}, 1000)
							},
						})
					}).finally(() => {
						uni.stopPullDownRefresh();
					})
				}
			}
		},
		onShow() {
			let info = getStorage('user');
			this.form.phone = info.mobile;
		}
	}
</script>

<style lang="scss">
	.uni-date-x {
		color: #000 !important;
	}

	.uni-date-x--border {
		border: none !important;
	}

	.uni-icons {
		display: none !important;
	}

	.uni-date__x-input {
		font-size: 32rpx !important;
	}

	.mine-page {
		overflow: hidden;

		.tips-block {
			background-color: #D6E0FF;
			color: #0256FF;
			width: calc(100% - 60rpx);
			display: flex;
			margin-top: 26%;
			margin-left: 30rpx;
			border-radius: 20rpx;
			padding: 30rpx;

			.iconfont {
				line-height: 60rpx;

				.tips-text {
					margin-left: 20rpx;
				}
			}
		}

		.info-block {
			padding: 30rpx;
			background-color: #F7F6F9;
			.tips{
				font-size: 28rpx;
				background-color: #D6E0FF;
				color: #0256FF;
				width: calc(100% - 20rpx);
				display: flex;
				margin-top: 20rpx;
				margin-bottom: 50rpx;
				margin: 0 auto;
				border-radius: 20rpx;
				padding: 30rpx;
			}
			.info-list {
				background-color: #fff;
				border-radius: 20rpx;
				padding: 30rpx;
				margin-bottom: 20rpx;

				.form-item:last-child {
					border-bottom: none;
				}

				.form-item {
					display: flex;
					flex-direction: column;
					border-bottom: 1px solid #F7F7F7;
					padding-bottom: 30rpx;
					margin-top: 30rpx;

					.name-item {
						display: flex;
						align-items: center;
						margin-bottom: 30rpx;

						.must {
							color: #EA0000;
						}

						.name {
							font-size: 30rpx;
						}
					}

					.form-content {
						display: flex;
						align-items: center;
						justify-content: space-between;
						width: 100%;


						.city {
							position: absolute;
							z-index: 3;
							padding-top: 120rpx;
							padding-left: 40rpx;
							color: #FFFFFF;
							display: flex;
							align-items: center;
							font-size: 30rpx;
						}

						.form-crl1 {
							width: calc(100% - 80rpx);
							font-size: 30rpx;
							overflow: hidden;
							text-overflow: ellipsis;
							white-space: nowrap;
						}

						.form-crl {
							width: 100%;
							font-size: 30rpx;
						}

						.right {
							display: flex;
							align-items: center;

							.iconfont {
								color: #CDCDCD;
								font-size: 32rpx;
							}
						}
					}

					.upload {
						display: flex;
						flex-direction: row;
						flex-wrap: wrap;

						.image-item {
							margin-top: 20rpx;
							margin-right: 20rpx;
							display: flex;
							flex-direction: row;
							// box-sizing: border-box;
							// border: 1px solid #efefef;

							.img {
								width: 190rpx;
								height: 200rpx;
								border-radius: 20rpx;
							}

							.iconfont {
								.delete {
									width: 20px;
									height: 20px;
									position: absolute;
									margin-left: -30rpx;
								}
							}
						}

						.image-item:nth-child(3n) {
							margin-right: 0;
						}

						.delete-item {
							width: 100%;
							display: flex;
							justify-content: flex-end;

							.delete {
								width: 20px;
								height: 20px;
								position: absolute;
							}
						}

						.image-item1 {
							display: flex;
							flex-direction: row;

							.delete {
								margin-top: 10rpx;
								margin-left: -40rpx;
								width: 20px;
								height: 20px;
								position: absolute;
							}
						}

						.upload-content {
							position: relative;
							background: #fafafa;
							border-radius: 5px;
							width: 200rpx;
							height: 200rpx;
							display: flex;
							align-items: center;
							justify-content: center;
							margin-top: 20rpx;

							.title {
								position: absolute;
								top: 50%;
								left: 50%;
							}

							.upload {
								width: 90rpx;
								height: 90rpx;
							}
						}
					}
				}
			}
		}

		.btn-item {
			display: flex;
			justify-content: center;
			background-color: #0256FF;
			color: #FFFFFF;
			border-radius: 60rpx;
			height: 80rpx;
			display: flex;
			align-items: center;
			width: 320rpx;
			margin: 30rpx auto;
		}
	}
</style>